<template>
  <div class="orderinvoice-edit">
    <el-dialog v-bind="$attrs" :title="state.dialogTitle" v-model="state.modalVisible" :before-close="cancelDialog"
      :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false"
      :lock-scroll="false" class="orderinvoice-dialog" draggable>
      <el-form ref="orderinvoiceRef" :model="state.form" :rules="state.rules" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="关联代理商id" prop="orderinvoiceagentsid">
              <el-input v-model="state.form.orderinvoiceagentsid" placeholder="关联代理商id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票抬头" prop="orderinvoicetoptype">
              <el-input v-model="state.form.orderinvoicetoptype" placeholder="发票抬头" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票类型" prop="orderinvoicetype">
              <el-input v-model="state.form.orderinvoicetype" placeholder="发票类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票状态" prop="orderinvoicestatus">
              <el-input v-model="state.form.orderinvoicestatus" placeholder="发票状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="orderinvoicegs">
              <el-input v-model="state.form.orderinvoicegs" placeholder="公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司税号" prop="orderinvoicesh">
              <el-input v-model="state.form.orderinvoicesh" placeholder="公司税号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="orderinvoicetel">
              <el-input v-model="state.form.orderinvoicetel" placeholder="联系方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户银行" prop="orderinvoiceyhmc">
              <el-input v-model="state.form.orderinvoiceyhmc" placeholder="开户银行" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行卡号" prop="orderinvoiceyhcode">
              <el-input v-model="state.form.orderinvoiceyhcode" placeholder="银行卡号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子邮箱" prop="orderinvoicemail">
              <el-input v-model="state.form.orderinvoicemail" placeholder="电子邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收件地址" prop="orderinvoiceaddress">
              <el-input v-model="state.form.orderinvoiceaddress" placeholder="收件地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票号码" prop="orderinvoicekpcode">
              <el-input v-model="state.form.orderinvoicekpcode" placeholder="开票号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票时间" prop="orderinvoicekpdate">
              <el-input v-model="state.form.orderinvoicekpdate" placeholder="开票时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票备注" prop="orderinvoicekpbz">
              <el-input v-model="state.form.orderinvoicekpbz" placeholder="开票备注" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="审核人" prop="orderinvoiceshuser">
              <el-input v-model="state.form.orderinvoiceshuser" placeholder="审核人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间" prop="orderinvoiceshdate">
              <el-input v-model="state.form.orderinvoiceshdate" placeholder="审核时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核备注" prop="orderinvoiceshbz">
              <el-input v-model="state.form.orderinvoiceshbz" placeholder="审核备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人员" prop="orderinvoicecreateuser">
              <el-input v-model="state.form.orderinvoicecreateuser" placeholder="创建人员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="orderinvoicecreatedate">
              <el-input v-model="state.form.orderinvoicecreatedate" placeholder="创建时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新时间" prop="orderinvoiceupdate">
              <el-input v-model="state.form.orderinvoiceupdate" placeholder="更新时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新人" prop="orderinvoiceupuser">
              <el-input v-model="state.form.orderinvoiceupuser" placeholder="更新人" />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useOrderinvoiceManageManageStore from '@store/modules/orderinvoiceManage'
import { filterObject } from '@/utils/index'
import { nextTick } from 'vue';

const orderinvoiceManageStore = useOrderinvoiceManageManageStore()
const { proxy } = getCurrentInstance()
const orderinvoiceRef = ref(null)

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    orderinvoiceagentsid: '',
    orderinvoicetoptype: '',
    orderinvoicetype: '',
    orderinvoicestatus: '',
    orderinvoicegs: '',
    orderinvoicesh: '',
    orderinvoicetel: '',
    orderinvoiceyhmc: '',
    orderinvoiceyhcode: '',
    orderinvoicemail: '',
    orderinvoiceaddress: '',
    orderinvoicekpcode: '',
    orderinvoicekpdate: '',
    orderinvoicekpbz: '',
    // orderinvoiceshuser:'',
    // orderinvoiceshdate:'',
    // orderinvoiceshbz:'',
    // orderinvoicecreateuser:'',
    // orderinvoicecreatedate:'',
    // orderinvoiceupdate:'',
    // orderinvoiceupuser:'',
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  },
})

const emits = defineEmits(['refresh'])

const getDataView = (orderinvoiceid) => {
  nextTick(async () => {
    const result = await orderinvoiceManageStore.view({ "orderinvoiceid.Value": orderinvoiceid })
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  orderinvoiceRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.orderinvoiceid = state.editView.orderinvoiceid
        await orderinvoiceManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await orderinvoiceManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      orderinvoiceRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  orderinvoiceRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.orderinvoiceid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.orderinvoicetoptype}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
